//
//  loginStat.m
//  MClient
//
//  Created by  史正烨 on 10-11-24.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "loginStat.h"
#import "UIDevice+IdentifierAddition.h"

@implementation loginStat
@synthesize partyId;
@synthesize userId;
@synthesize serverUrl;

static NSString*   statServerUrl = @"http://statistics.mobile086.cn/statistics/count";

- (void)dealloc {
	[partyId release];
	[userId release];
	[serverUrl release];
	
	[urlConnection cancel];
	[urlConnection release];
	[responseData release];
	[super dealloc];
}

- (void)sendStatRequest {
	[urlConnection cancel];
	[urlConnection release];urlConnection=nil;
	
	NSString *application = [NSString stringWithFormat:@"Iphone(%@)", 
							 [[MClientAppDelegate getInfoDictionary] objectForKey:@"CFBundleDisplayName"]];
	NSString *version = [[MClientAppDelegate getInfoDictionary] objectForKey:@"CFBundleVersion"];
	NSString *requestData = [NSString stringWithFormat:@"partyId=%@&userId=%@&serverAddress=%@&imei=%@&imsi=%@&series=%@&version=%@",
							 partyId?partyId:@"", userId?userId:@"", serverUrl?serverUrl:@"", 
							 [[UIDevice currentDevice] uniqueGlobalDeviceIdentifier], @"", application, version];
	const char *utfString = [requestData UTF8String];
	NSData   *postData = [NSData dataWithBytes:utfString length:strlen(utfString)];
	NSString *postLength = [NSString stringWithFormat:@"%d",[requestData length]];
	
	NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
	[request setURL:[NSURL URLWithString:statServerUrl]];
	[request setHTTPMethod:@"POST"];
	[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
	[request setHTTPBody:postData];
	
	[responseData release];
	responseData = [[NSMutableData data] retain];
	
	urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
	if (urlConnection) {
		[urlConnection cancel];
		[urlConnection release];
		urlConnection = nil;
	}
	NSLog(@"login stat Fail:%@", [error localizedDescription]);
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
	@try {
		[responseData setLength:0];
	}
	@catch (NSException * e) {
		NSLog(@"login stat didReceiveResponse Exception!!!!!:%@", [e reason]);
	}
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
	@try {
		[responseData appendData:data];
	}
	@catch (NSException * e) {
		NSLog(@"login stat didReceiveData Exception!!!!!:%@", [e reason]);
	}
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
	@try {
		NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
		if ([responseString isEqualToString:@"1"]) {
			NSLog(@"login stat Success");
		} else {
			NSLog(@"login stat Failed");
		}
		[responseString release];
	}
	@catch (NSException * e) {
		NSLog(@"login stat Exception!!!!!:%@", [e reason]);
	}
	@finally {
		if (urlConnection) {
			[urlConnection cancel];
			[urlConnection release];
			urlConnection = nil;
		}
	}
	
}

@end
